package com.atguigu.gmall.item.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.gmall.item.service.ItemService;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.item.ItemVo;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    ProductFeignClient productFeignClient;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public ItemVo getItem(Long skuId) {

        ItemVo itemVo = new ItemVo();

        /*RBloomFilter<Object> rbloomFilter = redissonClient.getBloomFilter(RedisConst.SKU_BLOOM_FILTER);

        if(!rbloomFilter.contains(skuId)) return itemVo;*/

        CompletableFuture<SkuInfo> skuCompletableFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            itemVo.setSkuInfo(skuInfo);
            return skuInfo;
        },threadPoolExecutor);

        //获取skuInfo+skuImageList
        CompletableFuture<Void> categoryViewCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            itemVo.setCategoryView(categoryView);
        },threadPoolExecutor);

        //跟新商品热点
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        },threadPoolExecutor);

        //获取价格信息
        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            itemVo.setSkuPrice(skuPrice);
        },threadPoolExecutor);

        //获取销售属性
        CompletableFuture<Void> attrListCompletableFuture = CompletableFuture.runAsync(() -> {
            List<BaseAttrInfo> attrList = productFeignClient.getAttrList(skuId);
            List<Map<String, String>> attrLists = attrList.stream().map(baseAttrInfo -> {
                Map<String, String> hashmap = new HashMap<>();
                hashmap.put("attrName", baseAttrInfo.getAttrName());
                hashmap.put("attrValue", baseAttrInfo.getAttrValueList().get(0).getValueName());
                return hashmap;
            }).collect(Collectors.toList());
            itemVo.setSkuAttrList(attrLists);
        });

        //销售属性值+锁定
        CompletableFuture<Void> skuSaleCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuSaleAttr> spuSaleAttrListCheckBySku = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            itemVo.setSpuSaleAttrList(spuSaleAttrListCheckBySku);
        },threadPoolExecutor);

        //用户销售属性实现切换sku
        CompletableFuture<Void> skuValueCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            Map skuValueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String skuValueIdMaps = JSON.toJSONString(skuValueIdsMap);
            itemVo.setValuesSkuJson(skuValueIdMaps);
        },threadPoolExecutor);

        //商品海报
        CompletableFuture<Void> spuPosterCompletableFuture = skuCompletableFuture.thenAcceptAsync(skuInfo -> {
            List<SpuPoster> spuPosterBySpuId = productFeignClient.getSpuPosterBySpuId(skuInfo.getSpuId());
            itemVo.setSpuPosterList(spuPosterBySpuId);
        },threadPoolExecutor);

        //多任务组合
        CompletableFuture.allOf(categoryViewCompletableFuture,
                                priceCompletableFuture,
                                attrListCompletableFuture,
                                skuSaleCompletableFuture,
                                skuValueCompletableFuture,
                                spuPosterCompletableFuture,
                                incrHotScoreCompletableFuture).join();
        
        return itemVo;
    }
}
